#pragma  comment(lib, "Ws2_32.lib")
#include <WinSock2.h>
#include <iostream>
using namespace std;

int main() {
    cout<<"----------基于UDP的聊天程序----------"<<endl;
	cout<<"-----客户端开发（客户端先说）------"<<endl;
	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD(2, 2);
	SOCKET cs;
	SOCKADDR_IN addr;
	char recvBuf[128];
	char sendBuf[128];
	int len = sizeof(sockaddr);
	if (WSAStartup(wVersionRequested, &wsaData) != 0) {
		cout<<"初始化DLL失败"<<endl;
		return 0;
	}
	cs = socket(AF_INET, SOCK_DGRAM, 0);
	addr.sin_family = AF_INET;
	addr.sin_port = htons(5000);
	addr.sin_addr.s_addr = inet_addr("111.117.58.38");
	while (1) {
		gets(sendBuf);
		sendto(cs, sendBuf, strlen(sendBuf)+1, 0, (sockaddr *)&addr, len);
		recvfrom(cs, recvBuf, 128, 0, (sockaddr *)&addr, &len);
		char *s_ip = inet_ntoa(addr.sin_addr);
		cout<<s_ip<<"说："<<recvBuf<<endl;
	}
	closesocket(cs);
	WSACleanup();
	return 0;
}